package com.directorio.modelo.dao;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.directorio.modelo.utiles.pojo.Funciones;
import org.springframework.mail.MailParseException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.directorio.modelo.bean.Email;
import com.directorio.modelo.dao.MailDao;

public class MailDaoImpl implements MailDao{
	
	private JavaMailSenderImpl mailSender;
    private SimpleMailMessage templateMessage;    

	public void setMailSender(JavaMailSenderImpl mailSender) {
		this.mailSender = mailSender;
	}
	public void setTemplateMessage(SimpleMailMessage templateMessage) {
		this.templateMessage = templateMessage;
	}


	public String sendMensaje(Email email) throws Exception {	
		MimeMessage message = mailSender.createMimeMessage();
		try{
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setFrom("bolsatrabajofii@gmail.com",email.getRemitente());
			helper.setTo(Funciones.getArrayStringFromList(email.getEmail()));
			helper.setSubject(email.getMensateSubject());
			helper.setText(email.getMensajeText(),true);
			helper.addAttachment("image", email.getImagen());
			mailSender.send(message);
		}catch (MessagingException e) {
		   e.printStackTrace();
		   return null;
		}
		return "ok";
	}
}
